import { PrinterDetailComponent } from './printerDetail/printerDetail.component';
import { AddPrinterComponent } from './addPrinter/addPrinter.component';
import { NavController } from 'ionic-angular';
import { DeviceManager } from './../../../common/mpj-deviceCenter/src/deviceManager';
import { Printer } from './../../../common/devices/printer/printer';
import { PrinterManager } from './../../../common/devices/printer/printerManager';
import { NavigateService } from './../../../service/navigate.service';
import { Component } from '@angular/core';

@Component({
    selector: 'printer',
    templateUrl: './printer.component.html',
    styleUrls: ['./printer.component.scss']
})

export class PrinterComponent {
    constructor(
        private router: NavigateService,
        public navCtrl: NavController
    ) { }
    printerManager;
    printers: any = [];

    ionViewWillEnter() {
        this.router.registGoBack(() => {
            let backBtn = document.getElementById("printer-back-btn");
            if (backBtn) {
                backBtn.click();
            }
        });
        this.printerManager = DeviceManager.Current.getManagerByType("printerManager");
        this.printers = this.printerManager.getAllDevices();
        if (Array.isArray(this.printers) && this.printers.length) {
            this.initPrinterName(this.printers);
        }
    }

    getAdsServerInfo() {

    }
    /**
     * 初始化打印机的名称
     * @param printers 
     */
    initPrinterName(printers) {
        this.printers.forEach((printer) => {
            if (printer.usage == "sales-receipt") {
                printer.name = "客户小票打印机";
            } else if (printer.usage == "kitchen-receipt") {
                printer.name = "厨房打印机";
            } else if (printer.usage == "kitchen-label") {
                printer.name = "标签打印机";
            }
        })
    }

    goBack() {
        // return this.router.navigate(["home"]);
        this.navCtrl.pop({ animate: false })
    }

    addPrinter() {
        // this.router.navigate(['addPrinter']);
        this.navCtrl.push(AddPrinterComponent, {}, { animate: false });
    }

    printerDetail(printer) {
        // this.router.navigate(['printerDetail', printer.id]);
        this.navCtrl.push(PrinterDetailComponent, {
            printerId: printer.id
        }, { animate: false })
    }
}